Module serverControl
    Sub stopServer()
        log.write("Shutting down the broadcast server...")
        broadcastSocket.Close()
        thrBroadcastListener.Abort()
        log.write("Attempting to close all client connections and abort threads")
        clients.closeAllConnections()
        clients.stopAllThreads()
        log.write("Done closing and aborting clients")
        log.write("Stopping server listener and stopping the thread")
        thrListener.Abort()
        serverSocket.Stop()
        thrAdminListener.Abort()
        adminSocket.Stop()
        sql.addToEventLog(sqlClient.eventLogType.i_ServerState, sqlClient.eventLogComponets.serverListener, "STOPPED - server stopped successfully!")
        log.write("Listener is stopped and thread aborted")
    End Sub

    Sub startServer()
        log.write("Starting to listen for broadcasts and start thread")
        broadcastSocket = New Net.Sockets.UdpClient(ern_broadcastPort)
        thrBroadcastListener = New Threading.Thread(AddressOf handleBroadcasts)
        thrBroadcastListener.Name = "Broadcast Server"
        thrBroadcastListener.Start()
        log.write("Starting to listen with a queue size of " & ern_serverQueueSize & " and start thread")
        serverSocket.Start(ern_serverQueueSize)
        thrListener = New Threading.Thread(AddressOf acceptConnections)
        thrListener.Name = "Listener"
        thrListener.Start()
        adminSocket.Start(ern_serverQueueSize)
        thrAdminListener = New Threading.Thread(AddressOf acceptAdminConnections)
        thrAdminListener.Name = "Admin Listener"
        thrAdminListener.Start()
        sql.addToEventLog(sqlClient.eventLogType.i_ServerState, sqlClient.eventLogComponets.serverListener, "STARTED - server started successfully!")
        log.write("Done starting listener and starting thread")
    End Sub
End Module
